package game

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	"navi-mumbai/common/gensql"
)

var _ ExchangeGameMoneyConfigModel = (*customExchangeGameMoneyConfigModel)(nil)

type (
	// ExchangeGameMoneyConfigModel is an interface to be customized, add more methods here,
	// and implement the added methods in customExchangeGameMoneyConfigModel.
	ExchangeGameMoneyConfigModel interface {
		exchangeGameMoneyConfigModel
		Find(ctx context.Context, pageNo, pageSize int64) ([]*ExchangeGameMoneyConfig, error)
		Count(ctx context.Context) (int64, error)
	}

	customExchangeGameMoneyConfigModel struct {
		*defaultExchangeGameMoneyConfigModel
		genSql *gensql.GenSql
	}
)

// NewExchangeGameMoneyConfigModel returns a model for the database table.
func NewExchangeGameMoneyConfigModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) ExchangeGameMoneyConfigModel {
	return &customExchangeGameMoneyConfigModel{
		defaultExchangeGameMoneyConfigModel: newExchangeGameMoneyConfigModel(conn, c, opts...),
		genSql:                              gensql.NewGenSql(),
	}
}

func (m *customExchangeGameMoneyConfigModel) Find(ctx context.Context, pageNo, pageSize int64) ([]*ExchangeGameMoneyConfig, error) {
	q := fmt.Sprintf("select %s from %s ", exchangeGameMoneyConfigRows, m.table)
	m.genSql.InitSql(q)
	query, values, err := m.genSql.OrderBy("create_time ASC").Limit(uint64(pageSize)).Offset(uint64((pageNo - 1) * pageSize)).ToSql()
	if err != nil {
		return nil, err
	}

	resp := make([]*ExchangeGameMoneyConfig, 0)
	err = m.QueryRowsNoCacheCtx(ctx, &resp, query, values...)
	switch err {
	case nil:
		return resp, nil
	default:
		return nil, err
	}
}

func (m *customExchangeGameMoneyConfigModel) Count(ctx context.Context) (int64, error) {
	query := fmt.Sprintf("select count(id) from %s ", m.table)
	var resp int64
	err := m.QueryRowNoCacheCtx(ctx, &resp, query)
	switch err {
	case nil:
		return resp, nil
	default:
		return 0, err
	}
}
